<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table {
        border: 1px solid #000;
        border-collapse: collapse;
      }
      th {
        background: #eee;
      }
      tr,
      th,
      td {
        padding: 8px 10px;
        border: 1px solid #000;
      }
      td > button {
        margin: 0 5px;
      }
    </style>
  </head>

  <body>
    <div id="app"></div>
    <!-- 1.引入依赖 -->
    <script src="../react/react.development.js"></script>
    <script src="../react/react-dom.development.js"></script>
    <script src="../react/babel.min.js"></script>
    <!-- 2.开始开发 -->
    <script type="text/babel">
      class App extends React.Component {
        constructor() {
          super()

          this.state = {
            books: [
              {
                id: 1,
                name: '《算法导论》',
                date: '2006-9',
                price: 85.0,
                count: 1,
              },
              {
                id: 2,
                name: '《UNIX编程艺术》',
                date: '2006-2',
                price: 59.0,
                count: 1,
              },
              {
                id: 3,
                name: '《编程珠玑》',
                date: '2008-10',
                price: 39.0,
                count: 1,
              },
              {
                id: 4,
                name: '《代码大全》',
                date: '2006-3',
                price: 128.0,
                count: 1,
              },
            ],
          }
        }

        render() {
          let totalPrice = this.state.books.reduce((preVal, item) => {
            return preVal + item.price
          }, 0)
          // console.log(totalPrice)
          return (
            <div>
              <table>
                <thead>
                  <tr>
                    <th></th>
                    <th>书籍名称</th>
                    <th>出版日期</th>
                    <th>价格</th>
                    <th>购买数量</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                  {this.state.books.map((item, index) => {
                    return (
                      <tr key={index}>
                        <td>{item.id}</td>
                        <td>{item.name}</td>
                        <td>{item.date}</td>
                        <td>{item.price}</td>
                        <td>
                          <button
                            disabled={item.count <= 1 && true}
                            onClick={e => this.count(index, -1)}
                          >
                            -
                          </button>
                          <span>{item.count}</span>
                          <button onClick={e => this.count(index, 1)}>+</button>
                        </td>
                        <td>
                          <button onClick={e => this.removeBook(index)}>
                            移除
                          </button>
                        </td>
                      </tr>
                    )
                  })}
                </tbody>
              </table>
              <h2>总价格: {totalPrice}</h2>
            </div>
          )
        }
        // 自增或自减
        count(index, count) {
          let newBooks = this.state.books
          newBooks[index].count += count
          this.setState({
            books: newBooks,
          })
        }
        // 移除书籍
        removeBook(index) {
          this.setState({
            books: this.state.books.filter((item, indey) => index !== indey),
          })
        }
      }
      ReactDOM.render(<App />, document.getElementById('app'))
    </script>
  </body>
</html>
